package com.example.front.serivce;

import com.example.common.dto.ListDto;
import com.example.common.dto.Pagination;
import com.example.common.exception.BusinessException;
import com.example.front.dto.BoxEventUserDto;
import com.example.front.entity.*;
import com.example.front.mapper.EventMapper;
import com.example.front.mapper.LevelMapper;
import com.example.front.mapper.MaintenanceMapper;
import com.example.front.mapper.MaintenanceUserMapper;
import com.example.interceptor.UserContext;
import com.example.passport.entity.User;
import com.example.passport.mapper.UserMapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class MaintenanceUserService {

    @Resource
    MaintenanceUserMapper maintenanceUserMapper;
    @Resource
    EventMapper eventMapper;
    @Resource
    UserMapper userMapper;
    @Resource
    LevelMapper levelMapper;
//    /**
//     * 创建设备
//     */
//    public void createEquipment(Equipment equipment) {
//        User me = UserContext.getUser();
//        equipmentMapper.create(equipment);
//    }

//    /**
//     * 跟新数据
//     */
//    public void update(@Valid Equipment equipment) {
//        User me = UserContext.getUser();
//        equipment.setTenantId(me.getTenantId());
//        if (equipmentMapper.update(equipment) != 1) {
//            throw new BusinessException("操作失败");
//        }
//    }

    /**
     * 删除
     */
    public void delete(long id) {
        User user = UserContext.getUser();
        if (maintenanceUserMapper.delete(id, user.getTenantId()) < 0) {
            System.out.println("MaintenanceUserServicedelete");
            throw new BusinessException("操作失败");
        }
    }

    /**
     * 查询单个
     */
    public List<BoxEventUserDto> findAll(Maintenance maintenance) {
        User user1 = UserContext.getUser();
//        maintenance.setTenantId(user1.getTenantId());
//        "event":[{"eventId":3,"user":{"17":"机加工"},"levelId":1},
//        {"eventId":8,"user":{"17":"机加工"},"levelId":1},
//        {"eventId":11,"user":{"1":"admin"},"levelId":1}]}
        List<MaintenanceUser> maintenanceUserList = maintenanceUserMapper.findAll(maintenance);
        List<Long> eventList = maintenanceUserMapper.group(maintenance);

        List<BoxEventUserDto> boxEventUserDtoList = new ArrayList<>();

        for (int i = 0; i < eventList.size(); i++) {
            Long eventId = eventList.get(i);
            BoxEventUserDto boxEventUserDto = new BoxEventUserDto();
            Map<Long, String> map = new HashMap<>();
            for (MaintenanceUser maintenanceUser : maintenanceUserList) {
                if (!eventId.equals(maintenanceUser.getEventId())) continue;
                //获取报警种类
                Event event = eventMapper.findById(maintenance.getTenantId(), maintenanceUser.getEventId());
                if (event == null) {
                    continue;
                }
                boxEventUserDto.setEventId(event.getId());
                //获取用户信息
                User user = userMapper.findById(maintenanceUser.getUserId());

                if (user == null) {
                    continue;
                }
                map.put(user.getId(), user.getName());

                boxEventUserDto.setLevelId(maintenanceUser.getLevelId());
            }
            boxEventUserDto.setUser(map);
            boxEventUserDtoList.add(boxEventUserDto);
        }

        return boxEventUserDtoList;
    }


    public void create(MaintenanceUser maintenanceUser) {
        if (maintenanceUserMapper.create(maintenanceUser) == 0) {
            System.out.println("MaintenanceUser-create");
            throw new BusinessException("操作失败");
        }
    }


    public List<MaintenanceUser> selectList(MaintenanceUser maintenanceUser) {
        return maintenanceUserMapper.selectList(maintenanceUser);
    }

}
